<?php include("../includes/header-admin.php") ?>
<title>LazySwim</title>
<link rel="stylesheet" href="store.css" type="text/css" />
</head>

<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">

                <div id="sidebar">
                    <?php
					// include the corresponding navigation page
                    include("../../includes/navigation.php");
					?>     
                </div>

    <!-- main body -->
 			<div class="featured-post"> 
			
				<div class="post-title"><h2>Order Status</h2></div>
				<div class="post-entry">
				
                <?php
                // Pull in the NuSOAP code
                require_once('../includes/lib/nusoap.php');

                // Create the client instance
                $client = new nusoap_client('http://consort.cs.ualberta.ca:41046/c410/projserv/order-server.php?wsdl', true);

                // Check for an error
                $err = $client->getError();
                if ($err) {
                    // Display the error
                    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
                    // At this point, you know the call that follows will fail
                }

                $result = array();
                // get each productID and quantity
                foreach( $_POST as $productID => $quantity ) {
                    // Call the SOAP method 
                    array_push($result, $client->call('order', array('groupID' => 4, 'productID' => $productID, 'quantity' => $quantity)));
                    
                    $sql = mysql_query("SELECT * FROM products WHERE productID = '$productID'") or die(mysql_error());
                    $product = mysql_fetch_assoc($sql);
                    
                    // Check for a fault
                    if ($client->fault) {
                        echo '<h2>Fault</h2><pre>';
                        print_r(end($result));
                        echo '</pre>';
                    } else {
                        // Check for errors
                        $err = $client->getError();
                        if ($err) {
                            // Display the error
                            echo '<h2>Error</h2><pre>' . $err . '</pre>';
                        } 
                        else {
                            $msg = end($result);
                            echo "Order for product: $product[name] (quantity: $quantity) result:" . $msg["retMsg"] . "<br><br>";
                        }
                    }
                }

                $totalCost = 0;
                $transactionID = 0;
                foreach( $result as $r ) {
                    // get the aggregate total cost
                    $totalCost += $r["price"];
                    // only take the transaction number of the last one
                    $transactionID = $r["trNumber"];
                }
                
                ?>

                <form name="checkoutForm" 
                action="http://consort.cs.ualberta.ca/~c410/paybuddy/payment.cgi?grp=4&amt=<?php echo sprintf("%01.2f", $totalCost); ?>&tx=<?php echo $transactionID; ?>&ret=http://www.hungmun.com/c410/site-gui/admin/store/listOrders.php" method="post"  target="_blank">
                     <input type="submit" name="submit" value="Pay for Orders">
                </form>
				<!-- end of main -->
				</div>
			</div>

                <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>
